<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/10/18
 * Time: 4:02 PM
 */

namespace app\zyhsapi\controller\v1;

use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\ZyhsCoupon;
use app\zyhsapi\model\ZyhsUserCoupon;
use think\response\Json;

class Coupon extends ZyhsAuth
{
    /**
     * 卡券中心
     * @return Json
     * @throws \Exception
     */
    public function index()
    {
        $type = input('type/d', 1);
        if (!in_array($type, [1, 2], true))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        $userCoupon = (new ZyhsUserCoupon())->getList($this->user['id'], $type);
        $cids = array_column($userCoupon['data'], 'c_id');
        $couponInfo = (new ZyhsCoupon())->batchGetInfoByIds($cids);
        $data = [];
        foreach ($userCoupon['data'] as $coupon)
        {
            $data[] = [
                'cid' => $coupon['id'],
                'name' => $couponInfo[$coupon['c_id']]['name'],
                'desc' => $couponInfo[$coupon['c_id']]['desc'],
                'time_des' => date('Y.m.d', $coupon['start_time']) . '-' . date('Y.m.d', $coupon['end_time']),
                'dtype' => $couponInfo[$coupon['c_id']]['type'],
                'discount' => $couponInfo[$coupon['c_id']]['discount'],
                'use_rule' => $couponInfo[$coupon['c_id']]['ur_des'],
            ];
        }
        $userCoupon['data'] = $data;
        return show(config('code.success'), 'Success.', $userCoupon);
    }
}
